package com;

import com.entity.ProductCodeEntity;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;

public class RunTime {
    public static void main(String[] args) throws IOException {
        List<String> lines = getConfig();
        ProductCodeEntity entity = initEntity(lines);
//		if(StringUtils.isEmpty(dbName)) {
//		 dbName= scannerLine(dbName, "请输入数据库名", lines, 0);
//		}
//		 tableName = scannerLine(tableName, "请输入表名", lines, 1);
//		 beforeCharacter = scannerLine(beforeCharacter, "请输入不保留的前缀", lines, 2);

        ProductCode code = new ProductCode();
        if (StringUtils.isEmpty(lines.get(0).trim())) {
            List<Map<String, String>> tables = code.getTables(lines.get(1).trim(), lines.get(4).trim(), lines.get(5).trim(), lines.get(6).trim());
            if (null != tables && tables.size() > 0) {
                for (Map<String, String> tableMap : tables) {
                    if (Objects.isNull(tableMap.get("TABLE_NAME")) || StringUtils.isEmpty(tableMap.get("TABLE_NAME").trim())) {
                        continue;
                    }

                    entity = initEntity(lines);
                    entity.setTable(tableMap.get("TABLE_NAME").trim());

                    System.out.println("-------------------------------------------------------------------------------------------------");
                    System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                    System.out.println("-------------------------------------------------------------------------------------------------");

                    ProductCode code1 = new ProductCode();
                    code1.choose(entity);
                }
            }
        } else if (StringUtils.indexOf(lines.get(0).trim(), ";") > 0) {
            String[] tables = StringUtils.split(lines.get(0).trim(), ";");
            for (String table : tables) {
                if (StringUtils.isEmpty(table)) {
                    continue;
                }
                System.out.println("-------------------------------------------------------------------------------------------------");
                System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
                System.out.println("-------------------------------------------------------------------------------------------------");

                ProductCode code1 = new ProductCode();

                entity = initEntity(lines);
                entity.setTable(table);

                code1.choose(entity);
            }
        } else {
            code.choose(entity);
        }
//		code.choose(tableName.trim(), dbName.trim(), beforeCharacter.trim(),serviceNameOfAfter);

//		Date date = new Date();
//		Date d = new Date(date.getTime()-1540202926L);
//		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//
//		System.out.println(dateformat.format(d));

    }

    private static ProductCodeEntity initEntity(List<String> lines) {
        ProductCodeEntity entity = new ProductCodeEntity();
        entity.setTable(lines.get(0).trim());
        entity.setDbName(lines.get(1).trim());
        entity.setBeforeCharacter(lines.get(2).trim());
        entity.setServiceNameOfAfter(lines.get(3).trim());
        entity.setIp(lines.get(4).trim());
        entity.setUser(lines.get(5).trim());
        entity.setPwd(lines.get(6).trim());
        entity.setBasePath(lines.get(7).trim());
        entity.setEntityStr(lines.get(8).trim());
        entity.setDtoStr(lines.get(9).trim());
        entity.setVoStr(lines.get(10).trim());
        entity.setMapperStr(lines.get(11).trim());
        entity.setMapperXMLPath(lines.get(12).trim());
        entity.setServiceStr(lines.get(13).trim());
        entity.setServiceImplStr(lines.get(14).trim());
        entity.setControllerStr(lines.get(15).trim());
        entity.setFeignStr(lines.get(16).trim());
        entity.setFeignService(lines.get(17).trim());
//        entity.setPageResultVOImpl(lines.get(18).trim());
        entity.setResponseDataImpl(lines.get(18).trim());
        entity.setConstant(lines.get(19).trim());
        entity.setResponseDateMethod(lines.get(20).trim());
        entity.setStartCharacter(lines.get(21).trim());
        entity.setDateUtils(lines.get(22).trim());
        entity.setDtoIng(lines.get(23));
        entity.setJsonConvertMethod(lines.get(24));
        entity.setIsDateType(lines.get(25));

        return entity;
    }

    private static List<String> getConfig() throws IOException {
        File file = new File("config.txt");
        System.out.println(file.getAbsolutePath());
        return FileUtils.readLines(file, "UTF-8");
    }

    private static String scannerLine(String val, String info, List<String> lists, int index) {
        val = switchValue(val, lists, index);
        if (StringUtils.isEmpty(val)) {
            Scanner sc = new Scanner(System.in);
            try {
                System.out.println(info);
                if (sc.hasNextLine()) {
                    val = sc.nextLine();
                }
                return StringUtils.isEmpty(val) ? "" : val.trim();
            } finally {
                sc.close();
            }
        }
        return val;
    }

    private static String switchValue(String val, List<String> lists, int index) {
        if (StringUtils.isEmpty(val) && lists.size() > index) {
            return lists.get(index).trim();
        }
        return val;
    }
}
